package org.example.sword2offer.primary;

import java.util.Arrays;

/**
 * @author: lynn
 * @Descript: 孩子们的游戏（圆圈中剩下的数）
 * @date: 2021/3/21 23:12
 * @version: 1.0
 */
public class Q46_LastRemaining {
    public static void main(String[] args) {
        System.out.printf(lastRemaining(5,3)+"");
    }
   // 约瑟夫环，公式：f[i] = (f[i-1]+m)%i   https://blog.csdn.net/u011500062/article/details/72855826
    public static int lastRemaining(int n, int m) {
        if (n<1 || m<1){
            return -1;
        }
        int temp=0;
        for (int i = 1; i <n+1 ; i++) {
            temp=(temp+m)%i;
        }
        return temp;
    }
}
